let redis = require('redis');
//const logger = require('pomelo-logger').getLogger('m-debug', __filename);

let Redis_db = function(app){
    const configs = app.get('redis');
    this.client = redis.createClient(configs.port, configs.host, configs.options);
    this.client.on('error', (error) => {
        //logger.error('redis 连接失败', error);
        console.error('redis 连接失败', error);
    });
    this.client.on('connect', (err,data) => {
        console.log('connect--------->');
        //logger.info(`${app.curServer.id} redis : connect`);
    });
    this.client.on('ready', async (err) => {
        //logger.info(`${app.curServer.id} redis : ready`, err);
        console.log('ready--->');
    });
    this.client.on("end", function () {
        console.log("与数据开断开连接");
    });
};
Redis_db.prototype.setAsync = function(key,data){
    return new Promise((resolve,reject)=>{
        this.client.set(key, data, (err, ret) => {
            if (err) {
                reject(err);
            } else {
                resolve(ret);
            }
        });
    });
};
Redis_db.prototype.getAsync = function(key,data){
    return new Promise((resolve,reject)=>{
        this.client.get(key, data, (err, ret) => {
            if (err) {
                reject(err);
            } else {
                resolve(ret);
            }
        });
    });
};
Redis_db.prototype.hgetallAsync = function(key){
    return new Promise((resolve,reject)=>{
        this.client.hgetall(key,(err,res)=>{
            if(err)
            {
                reject(err);
            }else
            {
                resolve(res);
            };
        });
    })
}
Redis_db.prototype.HMSETAsync = function(key,data){
    return new Promise((resolve,reject)=>{
        this.client.HMSET(key,data,(err,res)=>{
            if(err)
            {
                reject(err);
            }else
            {
                resolve(res);
            };
        });
    });
}
Redis_db.prototype.hgetFieldAsync = function(key,field){
    return new Promise((resolve,reject)=>{
        this.client.hget(key,field,(err,res)=>{
            if(err)
            {
                reject(err);
            }else
            {
                resolve(res);
            };
        });
    });
}
Redis_db.prototype.HMSETFieldAsync = function(key,field,value){
    return new Promise((resolve,reject)=>{
        this.client.HMSET(key,field,value,(err,res)=>{
            if(err)
            {
                reject(err);
            }else
            {
                resolve(res);
            };
        });
    });
}
Redis_db.prototype.delKeyAync = function(key){
    return new Promise((resolve,reject)=>{
        this.client.del(key,(err,res)=>{
            console.log('res',res)
            if(err)
            {
                reject(err);
            }else
            {
                resolve(res);
            };
        });
    })
}
module.exports = function(app){
    if(!this._reids_instance){
        this._reids_instance = new Redis_db(app);
    };
    return this._reids_instance;
};
